package com.wiloon.java8x.jsoup;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class JSoupTest {
    private static Logger logger = LogManager.getLogger(JSoupTest.class.getName());

    public static void main(String[] args) throws IOException {

        try {
            test();
        } catch (Exception ex) {
            logger.error(ex);
        }
    }

    public static void test1() {
        try {
            String html = "<html><head><title>Title</title></head>" + "<body><p> article </p></body></html>";
            html = "<head><title>Title</title></head>" + "<body><p> article </p>" +
                    "<a href='http://www.test.com'>test link</a>"+
                    "<table border=\"0\" class=\"tke_table normal\" >\n" +
                    "                        <tr><th class='heading'>{tr}Need installation manager input installation data{/tr}:</th></tr>\n" +
                    "                        <tr>\n" +
                    "                            <td>\n" +
                    "                                <input type=\"checkbox\" name=\"checkbox\" value=\"1\"/>Yes\n" +
                    "                            </td>\n" +
                    "                        </tr>\n" +
                    "                    </table>\n"+
                    "</body></html>";
            Document doc = Jsoup.parse(html);

            String title = doc.title();
            logger.debug("title=" +title);
            Elements links = doc.select("a[href]");
            logger.debug("link size="+links.size());
            Elements table = doc.select("table");
            logger.debug("table size="+table.size());
            table.addClass("normal");
            FileOutputStream fos = new FileOutputStream("JSoupTest.html", true);
            fos.write(doc.toString().getBytes());
            fos.close();
        } catch (Exception ex) {
            logger.error(ex);
        }
    }

    public static void test() {
        try {
            File input = new File("d:\\tmp\\test.tpl");
            Document doc = Jsoup.parse(input, "UTF-8", "http://www.test.com/");
            FileOutputStream fos = new FileOutputStream("JSoupTest.html", true);
            fos.write(doc.toString().getBytes());
            fos.close();
            logger.debug("title=" + doc.title());
        } catch (Exception ex) {
            logger.error(ex);
        }
    }

    public void temp() throws IOException {
        String html = "<html><head><title>title</title></head>" + "<body><p> article </p></body></html>";
        Document doc = Jsoup.parse(html);
        doc = Jsoup.connect("http://www.oschina.net/").get();
        String title = doc.title();

        doc = Jsoup.connect("http://www.oschina.net/")
                .data("query", "Java")
                .userAgent("I'm jsoup")
                .cookie("auth", "token")
                .timeout(3000)
                .post();
    }
}
